package com.gaogzhen.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaogzhen.domain.UserWallet;
import com.gaogzhen.service.UserWalletService;
import com.gaogzhen.utils.CurrentUser;
import com.gaogzhen.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author Administrator
 * @version 1.0
 * @description 会员地址管理器
 * @date 2022-06-08 11:35
 */
@RestController
@RequestMapping("/userWallets")
@Api(tags = "会员钱包管理器")
public class UserWalletController {

    @Autowired
    UserWalletService userWalletService;

    @GetMapping
    @ApiOperation("分页查询会员钱包列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页大小"),
            @ApiImplicitParam(name = "userId", value = "会员id")
    })
    @PreAuthorize("hasAuthority('user_wallet_query')")
    public R<Page<UserWallet>> findByPage(@ApiIgnore Page<UserWallet> page, Long userId) {
        return R.ok(userWalletService.findByPage(page, userId));

    }

    @GetMapping("/getCoinAddress/{coinId}")
    @ApiOperation("查询用户指定币种的提现地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "coinId", value = "币种Id"),
    })
    public R<List<UserWallet>> findUserWallets(@PathVariable Long coinId) {
        return R.ok(userWalletService.findUserWallets(CurrentUser.getCurrentUserId(), coinId));
    }

    @PostMapping
    @ApiOperation("新增用户指定币种的提现地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userWallet", value = "userWallet 的json信息"),
    })
    public R add(@RequestBody @Validated UserWallet userWallet) {
        userWalletService.add(CurrentUser.getCurrentUserId(), userWallet);
        return R.ok();
    }

    @DeleteMapping
    @ApiOperation("删除用户指定币种的提现地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userWallet", value = "userWallet 的json信息"),
    })
    public R delete(@RequestBody UserWallet userWallet) {
        userWalletService.delete(CurrentUser.getCurrentUserId(), userWallet);
        return R.ok();
    }
}
